package com.heima.thread.safe.sellTickets3_lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread{

    // 表示这个类所有的对象, 都共享ticket
    static int ticket = 0;

    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while(true)
        {
            lock.lock();

            try {
                if(ticket >= 1000){
                    break;
                }
                Thread.sleep(1);
                ++ticket;
                System.out.println(getName()+"正在卖第"+ticket+"张票");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }

        }
    }
}
